package com.tjetc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tjetc.annotion.Log;
import com.tjetc.common.Result;
import com.tjetc.entity.Admin;
import com.tjetc.service.IAdminService;
import com.tjetc.utils.JwtUtil;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private IAdminService adminService;
    @RequestMapping("/login")
    @Log(model = "登录",action = "验证账号密码")
    public Result<?> login(String username,String password){
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        QueryWrapper<Admin> qw=new QueryWrapper<>();
        qw.eq("username",username);
            Admin admin=adminService.getOne(qw);
        if (admin!=null){
            System.out.println("admin = " + admin);
            if(admin.getPassword().equals(password)){
                //返回登录成功信息
                String token= JwtUtil.createToken(admin.getUsername());
                Map<String,Object> map=new HashMap<>();
                map.put("token",token);
                map.put("admin",admin);
                return Result.success(map,"登录成功");
            }else{
                //返回密码错误信息
                return Result.fail("密码错误");
            }
        }else{
            //返回用户名不存在
            return Result.fail("用户名不存在");
        }
    }


    @GetMapping ("/checkToken")
    @Log(model = "登录",action = "Jwt验证token")
    public boolean checkToken(@RequestHeader("Token") String token) {
        System.out.println("token = " + token);
        boolean b= JwtUtil.isValid(token);
        if (b){
            System.out.println("b1= " + b);
            return true;
        }else{
            System.out.println("b2= " + b);
            return false;
        }
    }

    @RequestMapping("/logout")
    @Log(model = "登录",action = "退出登录")
    public boolean logout(){
        return true;
    }


}
